<?php


namespace backend\controllers;


use common\cache\DictCache;
use common\helpers\commonApi;
use common\models\Activity;
use common\models\ActivityClassify;
use common\models\ActivityEnter;
use common\models\ActivityEvaluate;
use common\models\ActivityLike;
use common\models\User;
use Yii;

class ActivityController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = Activity::className();
    }

    public function actionIndex()
    {

        $this->model = ActivityClassify::className();
        return parent::actionIndex();
    }

    public function actionAdd()
    {
        $this->model = ActivityClassify::className();
        return parent::actionAdd();
    }
    
    public function actionEdit()
    {
        $this->model = ActivityClassify::className();
        return parent::actionEdit();
    }

    public function actionList()
    {
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex(true,['delete'=>0]);
            $c_ids = array_column($data['data'],'c_id');
            $classify = ActivityClassify::find()->where(['id'=>$c_ids])->select(['id','name'])->asArray()->all();
            $classify = array_column($classify,'name','id');
            foreach ($data['data'] as &$datum){
                $datum['c_id'] = $classify[$datum['c_id']]??'';
                $datum['start_time'] = date('Y-m-d',strtotime($datum['start_time']));
                $datum['end_time'] = date('Y-m-d',strtotime($datum['end_time']));
            }
            return $data;
        }
        return parent::actionIndex();
    }

    public function actionListAdd()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $times = explode('~',$post['times']);
            $post['start_time'] = trim($times[0]);
            $post['end_time'] = trim($times[1]);
            $post['surplus'] = $post['total']??0;
            if ($post['is_boutique']){
                if (empty($post['recommend_img'])){
                    return commonApi::message('请上传首页推荐图',false);
                }
            }
            $model = new Activity();
            $model->load($post,'');
            if ($model->validate()&&$model->save()){
                return commonApi::message('添加成功');
            }
            return commonApi::message(commonApi::getModelError($model),false);
        }
        $classify = ActivityClassify::find()->where(['status'=>1])->select(['id','name'])->asArray()->all();
        $classify = array_column($classify,'name','id');
        Yii::$app->view->params['classify']= $classify;
        return parent::actionAdd();
    }

    public function actionListEdit()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id = $post['id']??'';
            $model = Activity::findOne($id);
            if (!$model){
                return commonApi::message('活动不存在',false);
            }
            $times = explode('~',$post['times']);
            $post['start_time'] = trim($times[0]);
            $post['end_time'] = trim($times[1]);
            $post['surplus'] = $post['total']??0;
            if ($post['is_boutique']){
                if (empty($post['recommend_img'])){
                    return commonApi::message('请上传首页推荐图',false);
                }
            }
            $model->load($post,'');
            if ($model->validate()&&$model->save()){
                return commonApi::message('添加成功');
            }
            return commonApi::message(commonApi::getModelError($model),false);
        }
        $classify = ActivityClassify::find()->where(['status'=>1])->select(['id','name'])->asArray()->all();
        $classify = array_column($classify,'name','id');
        Yii::$app->view->params['classify']= $classify;
        return parent::actionEdit();
    }

    /**
     * 报名
     * @return array|string
     */
    public function actionEnter()
    {
        $this->model = ActivityEnter::className();
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex();
            $userid = array_column($data['data'],'userid');
            $user = User::find()->where(['id'=>$userid])->select(['id','info'])->asArray()->all();
            $html = (new User())->getUserHtml($user);
            foreach ($data['data'] as &$datum){
                $info = json_decode($datum['info'],true);
                $datum['act_info'] = "<div><div><img style='width: 100px' src='{$info['main_img']}' alt=''></div><div>{$info['name']}</div></div>";
                $datum['start_time'] = date('Y-m-d',strtotime($info['start_time']));
                $datum['end_time'] = date('Y-m-d',strtotime($info['end_time']));
                $datum['user'] = $html[$datum['userid']]??'';
            }
            return $data;
        }
        $pay_status = DictCache::get('sys_activity_pay_status');
        Yii::$app->view->params['pay_status']= $pay_status;
        return parent::actionIndex();
    }

    public function actionEvaluate()
    {
        $this->model = ActivityEvaluate::className();
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex();
            $userid = array_column($data['data'],'userid');
            $user = User::find()->where(['id'=>$userid])->select(['id','info'])->asArray()->all();
            $html = (new User())->getUserHtml($user);
            foreach ($data['data'] as &$datum){
                $info = json_decode($datum['info'],true);
                $datum['act_info'] = "<div><div><img style='width: 100px' src='{$info['main_img']}' alt=''></div><div>{$info['name']}</div></div>";
                $datum['start_time'] = date('Y-m-d',strtotime($info['start_time']));
                $datum['end_time'] = date('Y-m-d',strtotime($info['end_time']));
                $datum['user'] = $html[$datum['userid']]??'';
            }
            return $data;
        }
        $pay_status = DictCache::get('sys_review_status');
        Yii::$app->view->params['review_status']= $pay_status;
        return parent::actionIndex();
    }

    public function actionReview()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $id = $post['id']??'';
            $model = ActivityEvaluate::findOne($id);
            if (!$model){
                return commonApi::message('参数错误',false);
            }
            $model->review_status = $post['review_status'];
            $model->review_remarks = $post['review_remark'];
            $model->review_time = date('Y-m-d H:i:s');
            if ($model->save()){
                return commonApi::message('操作成功');
            }
            return commonApi::message(commonApi::getModelError($model),false);
        }
        $id = Yii::$app->request->get('id','');
        $model = ActivityEvaluate::findOne($id);
        if (!$model){
            return $this->bError('参数错误');
        }
        $review_status = DictCache::get('sys_review_status');
        unset($review_status[0]);
        return $this->render('',['review_status'=>$review_status,'info'=>$model->toArray()]);
    }

    public function actionLike()
    {
        $this->model = ActivityLike::className();
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex();
            $userid = array_column($data['data'],'userid');
            $user = User::find()->where(['id'=>$userid])->select(['id','info'])->asArray()->all();
            $html = (new User())->getUserHtml($user);
            foreach ($data['data'] as &$datum){
                $datum['user'] = $html[$datum['userid']]??'';
            }
            return $data;
        }
        return parent::actionIndex();
    }

    public function actionDrop()
    {
        return parent::actionDrop();
    }

    public function actionEvaluateDrop()
    {
        $this->model = ActivityEvaluate::className();
        return parent::actionDrop();
    }

    public function actionEvaluateDetails()
    {
        $this->model = ActivityEvaluate::className();
        $pay_status = DictCache::get('sys_review_status');
        Yii::$app->view->params['review_status']= $pay_status;
        return parent::actionDetails();
    }
}